﻿using System;
using LINQ2TextFile;
using log4net;
namespace WPFExcelReport
{
    
    public class ITXPrint
    {
        public log4net.ILog m_log_out = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        const uint IMAGE_BITMAP = 0;
        const uint LR_LOADFROMFILE = 16;
       public string szSavePath = "C:\\Argox";
       public string szSaveFile = "C:\\Argox\\WBD10IT.PRN";
       public string printer = @"\\192.168.1.156\TERM156";
       public bool Is2Printer = true;
       public string sznop1 = "nop_front\r\n";
       public string sznop2 = "nop_middle\r\n";
       string absfile = "";
       public string printorfile = "";
        public int copys = 1;
        public ITXPrint()
        {
           
        }
        public void ExecutePrint(string file = "")
        {
            if (file == "")
                return;
            if (!System.IO.File.Exists(file))
                return;
            szSaveFile = file;
            int start = file.IndexOf("#", 1);
            int end = file.IndexOf(".ITXTTF", 1);
            printer = file.Substring(start,end-start);
            absfile = file.Substring(0, start);
            printer = printer.Replace("#", @"\");
            copys = 1;
            string filenew = "";
            switch (printorfile)
            { 
                case "file":
                    filenew=CreateTransFile();
                    break;
                case"print":
                    Print();
                    break;
                case"filecopy": 
                default:
                    filenew=CreateTransFile();
                    string cmd = "Copy /B " + filenew + " " + printer + ">Nul";
                    DosCommand.ExecuteCommandAsync(cmd);
                    break;
            }
            
            System.IO.File.Delete(file);
        }
        public string CreateTransFile()
        {
            string filenew = "";
            try
            {
                //Test code start
                // open port.
                int ret;
                System.Text.Encoding encAscII = System.Text.Encoding.ASCII;
                System.Text.Encoding encUnicode = System.Text.Encoding.Unicode;
                //ITXTTFAPI.B_Set_Prncomport_PC(0, 8, 0, 1, 1, 1, 1);
                //ret = ITXTTFAPI.B_CreatePrn(10, printer);
                filenew = absfile + "txt";
                if (System.IO.File.Exists(filenew))
                { 
                    System.IO.File.Delete(filenew);
                }
                ret = ITXTTFAPI.B_CreatePrn(0, filenew);// open file.   
                // sample setting.
                ITXTTFAPI.B_Set_DebugDialog(1);
                ITXTTFAPI.B_Set_Originpoint(0, 0);
                ITXTTFAPI.B_Select_Option(2);
                ITXTTFAPI.B_Set_Darkness(8);
                ITXTTFAPI.B_Del_Pcx("*");// delete all picture.
                ITXTTFAPI.B_WriteData(0, encAscII.GetBytes(sznop2), sznop2.Length);
                ITXTTFAPI.B_WriteData(1, encAscII.GetBytes(sznop1), sznop1.Length);
                StartPrint(szSaveFile);
                ITXTTFAPI.B_Print_Out(copys);// copy 2. 
                ITXTTFAPI.B_ClosePrn();
            }
            catch (Exception ex)
            {
            }
            return filenew;
        }
        public void Print()
        {
            //Test code start
            // open port.
            System.Text.Encoding encAscII = System.Text.Encoding.ASCII;
            System.Text.Encoding encUnicode = System.Text.Encoding.Unicode;
            int ret;
            //ITXTTFAPI.B_Set_Prncomport_PC(0, 8, 0, 1, 1, 1, 1);
            ret = ITXTTFAPI.B_CreatePrn(10, printer);
            // sample setting.
            ITXTTFAPI.B_Set_DebugDialog(1);
            ITXTTFAPI.B_Set_Originpoint(0, 0);
            ITXTTFAPI.B_Select_Option(2);
            ITXTTFAPI.B_Set_Darkness(8);
            ITXTTFAPI.B_Del_Pcx("*");// delete all picture.
            ITXTTFAPI.B_WriteData(0, encAscII.GetBytes(sznop2), sznop2.Length);
            ITXTTFAPI.B_WriteData(1, encAscII.GetBytes(sznop1), sznop1.Length);
            StartPrint(szSaveFile);
            ITXTTFAPI.B_Print_Out(copys);// copy 2. 
            ITXTTFAPI.B_ClosePrn();
        }
        public void StartPrint(string file4ex)
        {
            try
            {
                string command = "";
                TextFileReader reader1 = new TextFileReader(file4ex, ","); 
                int n7 = 1;
                int key = 1;
                int x1 = 0, y1 = 0, height = 0, width = 0, x2=0, y2 = 0;
                int thick = 1;
                string fontname="宋体";
                string str = "";
                int fontsize;
               
                //System.Text.Encoding encUnicode = System.Text.Encoding.Unicode;
                System.Text.Encoding encUTF8 = System.Text.Encoding.UTF8;
                //System.Text.Encoding encGB2312= System.Text.Encoding.GetEncoding("GB2312");
                foreach (var text in reader1)
                {
                    command = text[0];
                    switch (command[0].ToString())
                    {
                        case "X":
                            //X20,20,4,380,280
                            // A30,40,0,2,1,1,N,"Article:"
                                //public static extern int B_Draw_Box(int x, int y, int thickness, int hor_dots,int ver_dots);
                            x1 = System.Convert.ToInt32(text[1]);
                            y1 = System.Convert.ToInt32(text[2]);
                            thick = System.Convert.ToInt16(text[3]);
                            width = System.Convert.ToInt32(text[4]);
                            height = System.Convert.ToInt32(text[5]);
                            ITXTTFAPI.B_Draw_Box(x1, y1, thick, width, height);
                            break;
                        case "H":
                            //H,180,95,1020,180
                            x1 = System.Convert.ToInt32(text[1]);
                            y1 = System.Convert.ToInt32(text[2]);
                            width = System.Convert.ToInt32(text[3]);
                            height = System.Convert.ToInt32(text[4]);
                            ITXTTFAPI.B_Draw_Line('E', x1, y1, width, height);
                            break;                            
                        case "A":
                            //A,微软雅黑,12,190,50,0,4,1,2,N,品种Art:
                            fontname = text[1];
                            fontsize = System.Convert.ToInt16(text[2]);
                            //fontsize = 4 * fontsize;
                            x1 = System.Convert.ToInt32(text[3]);
                            y1 = System.Convert.ToInt32(text[4]);
                            //p5 = System.Convert.ToInt32(text[4]);
                            //p6 = System.Convert.ToInt32(text[5]);
                            //p7 = text[6];
                            str = "";
                            for (int i = 10; i < text.Length;i++ )
                            {
                                str += text[i].ToString()+",";
                            }
                            str += "***";
                            str = str.Replace(",***", "");
                            int intyd = str.IndexOf("YD");
                            string remains = "";
                            string pre = "";
                            int blank = str.IndexOf(": ");
                            string lengyd = "0";
                            double meter = 0;
                            int indm=0;
                            int indl=0;
                            if (intyd > 0)
                            {
                                lengyd = str.Substring(blank + 1, intyd - blank - 1).Replace(",", ".");
                                remains = str.Substring(intyd + 3, str.Length - intyd - 3);
                                indm= remains.IndexOf("m");
                                if (indm > 0)
                                {
                                    meter = System.Convert.ToDouble(remains.Substring(1, indm - 1));
                                }
                                else
                                    meter = 0;
                                pre = str.Substring(0, blank + 1);
                            }
                            if (str.Contains("YD0"))
                            {
                                if(meter>0)
                                remains="("+System.Math.Round(meter,0)+"m)";
                                str = pre + System.Math.Round(System.Convert.ToDecimal(lengyd), 0) + "yd" + remains;
                            }
                            if (str.Contains("YD1"))
                            {
                                if (meter > 0)
                                remains="("+System.Math.Round(meter,1)+"m";
                                str = pre + System.Math.Round(System.Convert.ToDecimal(lengyd), 1) + "yd" + remains;
                            }
                            if (str.Contains("YD2"))
                            {
                                if (meter > 0)
                                remains = "(" + System.Math.Round(meter, 2) + "m";
                                str = pre + System.Math.Round(System.Convert.ToDecimal(lengyd), 2) + "yd" + remains;
                            }
                            str = str.Replace("\"", "");
                            //p8 = text[7].ToString().Replace(@"""", "");
                            //ITXTTFAPI.B_Prn_Text(p1, p2, p3, p4, p5, p6, 'N', p8);
                            //B_Prn_Text_TrueType_Uni(30,220,30,"Times New Roman",1,400,0,0,0,"AC",Encoding.Unicode.GetBytes("TT_Uni: 多字元測試"),1);//UTF-16
                            key++;
                            //ITXTTFAPI.B_Prn_Text_TrueType_Uni(p1,p2,30,"Times New Roman",1,400,0,0,0,"C" + key.ToString(),encUnicode.GetBytes(p8),1);//UTF-16
                           //ITXTTFAPI.B_Prn_Text_TrueType_Uni(p1,p2,30,"Times New Roman",1,400,0,0,0,"C" + key.ToString(),encUTF8.GetBytes(p8),3);//UTF-8
                             ITXTTFAPI.B_Prn_Text_TrueType(x1, y1, fontsize, fontname, 1, 400, 0, 0, 0, "C" + key.ToString(), str);//save in printer.
                            break;
                        case "B":
                            //B,250,390,0,3,4,8,100,B,\48
                            //B_Prn_Barcode(int x, int y, int ori, string type, int narrow,int width, int height, char human, string data);
                            x1 = System.Convert.ToInt32(text[1]);
                            y1 = System.Convert.ToInt32(text[2]);
                            int ori = System.Convert.ToInt32(text[3]);
                            string type = text[4];
                            int narrow = System.Convert.ToInt32(text[5]);
                            width = System.Convert.ToInt32(text[6]);
                            height = System.Convert.ToInt32(text[7].ToString());
                            char human = text[8].ToCharArray()[0];
                            str = text[9].ToString().Replace(@"""", "");
                            ITXTTFAPI.B_Prn_Barcode(x1, y1, ori, type, narrow, width, height, human, str);
                            break;
                        case "b":
                            //b30,200,Q,m1,s3,eM,iA,"0053 33 -59"
                            //B_Bar2d_QR(int x, int y, int model, int scl, char error,char dinput, int c, int d, int p, string data);                           
                            x1 = System.Convert.ToInt32(text[1]);
                            y1 = System.Convert.ToInt32(text[2]);
                            /*p3 = System.Convert.ToInt16(text[2]);
                            p4 = System.Convert.ToInt16(text[3]);
                            p5 = System.Convert.ToInt16(text[4]);
                            p6 = System.Convert.ToInt16(text[5]);
                            n7 = System.Convert.ToInt16(text[6]);*/
                            str = text[8].ToString().Replace(@"""", "");
                            ITXTTFAPI.B_Bar2d_QR(x1, y1, 1, 5, 'M', 'A', 0, 0, 0, str);
                            //ITXTTFAPI.B_Prn_Barcode(0, 0, 0, "0", 2, 3, 60, 'B', p8);//have a counter
                            //ITXTTFAPI.B_Prn_Barcode(p1, p2, 1, "0", 2, 3, 40, 'B', p8);//have a counter
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                //Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
